package com.br.android.widing.db;

import com.br.android.widing.entity.Gasto;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class WidingDAO extends WidingDataBase{

	public WidingDAO(Context context) {
		super(context);
	}

	public WidingCursor getAll(){
		String sql = WidingCursor.QUERY;
		SQLiteDatabase dataBase = getReadableDatabase();
        WidingCursor c = (WidingCursor) dataBase.rawQueryWithFactory(
                        new WidingCursor.Factory(), sql, null, null);
        c.moveToFirst();
        return c;
	}
	
	public WidingCursor getTotalValores(){
		String sql = WidingCursor.QUERY_SOMA_VALORES;
		SQLiteDatabase dataBase = getReadableDatabase();
		WidingCursor c = (WidingCursor) dataBase.rawQueryWithFactory(new WidingCursor.Factory(), sql, null, null);
		c.moveToFirst();
		return c;
	}
	
	public WidingCursor getTotalValoresPorCategoria(){
		String sql = WidingCursor.QUERY_SOMA_VALORES_POR_CATEGORIA;
		SQLiteDatabase dataBase = getReadableDatabase();
		WidingCursor c = (WidingCursor) dataBase.rawQueryWithFactory(new WidingCursor.Factory(), sql, null, null);
		c.moveToFirst();
		return c;
	}
	
	public void inserirGasto(Gasto gasto){
		String sql = "INSERT INTO gasto(nomegasto, valorgasto, categoriagasto, datagasto) VALUES(?,?,?,?)";
        Object[] obj = new Object[] { gasto.getNomeGasto(), gasto.getValor(),
                        gasto.getCategoria(), gasto.getDataCadastro()};
        try {
                getWritableDatabase().execSQL(sql, obj);
        } catch (Exception e) {
                Log.e("Erro ao inserir um novo gasto", e.toString());
        }
	}
	
	public void excluirGasto(int id){
		String sql = "DELETE FROM gasto WHERE idgasto = ?";
		Object[] obj = new Object[]{id};
		try {
			getWritableDatabase().execSQL(sql, obj);
		} catch (Exception e) {
			Log.e("Erro ao excluir um gasto", e.toString());
		}
	}
	
	public void alterarGasto(Gasto gasto){
		String sql = "UPDATE gasto SET nomegasto = ?, valorgasto = ?, categoriagasto = ?, datagasto = ? WHERE idgasto = ?";
		Object[] obj = new Object[]{gasto.getNomeGasto(), gasto.getValor(), 
									gasto.getCategoria(), gasto.getDataCadastro(), gasto.getIdGasto()};
		try {
			getWritableDatabase().execSQL(sql, obj);
		} catch (Exception e) {
			Log.e("Erro ao alterar um gasto", e.toString());
		}
	}
	
}
